﻿#pragma once

#include "TextInputInterface.h"

#include <Windows.h>
#include <imm.h>
#include <iostream>


//包装了windows的输入法编辑器
//这里的函数必须在创建窗口的线程中调用
class CWindowsInputMethodEditor : public CTextInputInterface
{
public:
    CWindowsInputMethodEditor()
    {
        //create ime context
        hIMC = ImmCreateContext();
    }

    ~CWindowsInputMethodEditor()
    {
        //destroy ime context
        ImmDestroyContext(hIMC);
    }

    virtual void StartTextInput(uint64_t WindowID, int64_t IMEWidgetPositionX,int64_t IMEWidgetPositionY) override
    {
        //open ime
        ImmAssociateContext((HWND)WindowID, hIMC);

        //set ime position
        POINT pt = { (LONG)IMEWidgetPositionX,(LONG)IMEWidgetPositionY };
        //ClientToScreen((HWND)WindowID, &pt);
        COMPOSITIONFORM cf;
        cf.dwStyle = CFS_FORCE_POSITION;
        cf.ptCurrentPos = pt;
        ImmSetCompositionWindow(hIMC, &cf);

        InputingWindow = (HWND)WindowID;
    }
    virtual void StopTextInput()  override
    {
        //close ime
        ImmAssociateContext(InputingWindow, NULL);
        InputingWindow = 0;

    }

    virtual bool IsTextInputStarted() const  override
    {
        return InputingWindow != NULL;
    }

protected:
    HIMC hIMC;
    HWND InputingWindow= NULL;

};